<?php
/**
 * Create By: PhpStorm
 * User: yuanxuncheng
 * File: GetOrderDetail.php
 * Date: 2017/1/2
 * Time: 23:13
 */

namespace app\api\model\page\order;

use app\api\model\dao\Goods;
use app\api\model\dao\OrderGoods;
use app\api\model\dao\OrderInfo;
use app\api\model\dao\Region;
use app\library\Error;
use app\library\PageBase;
use think\Validate;
use think\Cache;
use app\library\Flag;

class GetOrderDetail extends PageBase
{

    private $objOrderInfo;
    private $objOrderGoods;
    private $objGoods;
    private $objRegion;

    public function __construct()
    {
        parent::__construct();
        $this->objOrderInfo = new OrderInfo();
        $this->objOrderGoods = new OrderGoods();
        $this->objGoods = new Goods();
        $this->objRegion = new Region();
    }

    /**
     * @param $param
     * @return array|bool
     * @DESC 验证接口字段
     */
    public function checkParam($param)
    {
        $rule = [
            'order_id' => 'require',
        ];
        $msg = [
            'order_id.require' => '订单ID必填',
        ];

        $validate = new Validate($rule, $msg);
        $result = $validate->check($param);
        if (!$result) {
            return $validate->getError();
        }
        return '';
    }

    public function excute($param)
    {
        $ret = $this->checkToken($param);
        if (false === $ret) {
            return $this->getRes(Error::ERR_NO_AUTH);
        }

        // TODO:业务逻辑
        // 1、参数校验
        $ret = $this->checkParam($param);
        if ($ret) {
            return $this->getRes(Error::ERR_PARAM, $ret);
        }

        // 获取订单信息
        $order_id = $param['order_id'];
        $user_id = $this->user_id;

        $orderInfo = $this->objOrderInfo->getOrderInfo($order_id);
        if (false === $orderInfo) {
            return $this->getRes(Error::ERR_SYS);
        }

        $goods_ids = [];
        // 获取商品信息
        $orderGoods = $this->objOrderGoods->getOrderGoods($order_id);
        $goods_ids = array_map(function ($v) {
            return $v['goods_id'];
        }, $orderGoods);
        $goods = $this->objGoods->getGoods(implode(',', $goods_ids));
        foreach ($goods as $good) {
            $goods_arr[$good['goods_id']] = $good;
        }

        foreach ($orderGoods as &$v) {
            if (isset($goods_arr[$v['goods_id']])) {
                $v['market_price'] = $goods_arr[$v['goods_id']]['market_price'];
                $v['shop_price'] = $goods_arr[$v['goods_id']]['shop_price'];
                $v['sale_count'] = $goods_arr[$v['goods_id']]['sale_count'];
                // 加上折扣
                $v['discount'] = '' . round($goods_arr[$v['goods_id']]['shop_price'] / $goods_arr[$v['goods_id']]['market_price'], 2) * 10;
                $v['goods_thumb'] = $goods_arr[$v['goods_id']]['goods_thumb'];
            }
        }


        // 获取地址信息
        $region = [];
        $address = [];
        $address['consignee'] = isset($orderInfo['consignee']) ? $orderInfo['consignee'] : '';
        unset($orderInfo['consignee']);

        $region[] = isset($orderInfo['country']) ? $orderInfo['country'] : '';
        unset($orderInfo['country']);

        $region[] = isset($orderInfo['province']) ? $orderInfo['province'] : '';
        unset($orderInfo['province']);

        $region[] = isset($orderInfo['city']) ? $orderInfo['city'] : '';
        unset($orderInfo['city']);

        $region[] = isset($orderInfo['district']) ? $orderInfo['district'] : '';
        unset($orderInfo['district']);

        $address['address'] = isset($orderInfo['address']) ? $orderInfo['address'] : '';
        unset($orderInfo['address']);

        $address['zipcode'] = isset($orderInfo[0]['zipcode']) ? $orderInfo[0]['zipcode'] : '';
        unset($orderInfo['zipcode']);

        $address['tel'] = isset($orderInfo['tel']) ? $orderInfo['tel'] : '';
        unset($orderInfo['tel']);

        $address['mobile'] = isset($orderInfo[0]['mobile']) ? $orderInfo[0]['mobile'] : '';
        unset($orderInfo['mobile']);

        $address['email'] = isset($orderInfo['email']) ? $orderInfo['email'] : '';
        unset($orderInfo['email']);

        // 填补地址信息
        $regions = $this->objRegion->getRegion(implode(',', $region));
        if (!empty($regions)) {
            foreach ($regions as $region) {
                if ($region['region_type'] === 0) {
                    $address['country'] = $region['region_name'];
                }
                if ($region['region_type'] === 1) {
                    $address['province'] = $region['region_name'];
                }
                if ($region['region_type'] === 2) {
                    $address['city'] = $region['region_name'];
                }
                if ($region['region_type'] === 3) {
                    $address['district'] = $region['region_name'];
                }
            }
        }

        $order_status = $orderInfo['order_status'];
        $order_status_desc = Flag::$map_order[$order_status];
        $shipping_partername = $orderInfo['shipping_partername'];
        $shipping_no = $orderInfo['shipping_no'];
        $contact_phone = $orderInfo['contact_phone'];

        $goods_amount = $orderInfo['goods_amount'];
        $order_amount = $orderInfo['order_amount'];

        $this->data = array(
            'order_info' => $orderInfo,
            'order_goods' => $orderGoods,
            'address' => $address,
            'settlement' => [
                'goods_amount_sum' => round($goods_amount, 2),
                'need_pay' => round($order_amount, 2),
                'shipping_fee' => 0,
                'shipping_fee_reduction' => 0,
                'coupon' => 0,
                'mandou' => 0
            ],

            'order_status' => $order_status,
            'order_status_desc' => $order_status_desc,
            'shipping_partername' => $shipping_partername,
            'shipping_no' => $shipping_no,
            'contact_phone' => $contact_phone,
        );

        return $this->getRes();
    }
}
